{
 "metadata": {
  "name": "",
  "signature": "sha256:727aae4880c544aaf9328e1228279e165380f9abb68d15da0e5f4178524e4a01"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Note"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "This notebook had been used for internal testing of some of nilmtk features. Please don't use this notebook!"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from __future__ import print_function\n",
      "from copy import deepcopy\n",
      "\n",
      "\n",
      "class DataStore(object):\n",
      "    def __init__(self, mask=None):\n",
      "        self.mask = [] if mask is None else mask\n",
      "    \n",
      "    def state_of_data(self):\n",
      "        return {}\n",
      "    \n",
      "    def power_series(self):\n",
      "        for i in range(10):\n",
      "            if i not in self.mask:\n",
      "                yield i\n",
      "                \n",
      "    def process(self):\n",
      "        return self.power_series()\n",
      "\n",
      "    \n",
      "class FailedPreconditionError(Exception):\n",
      "    pass\n",
      "\n",
      "\n",
      "class Node(object):\n",
      "    def __init__(self, upstream):\n",
      "        self.upstream = upstream\n",
      "        \n",
      "    def state_of_data(self):\n",
      "        upstream_condition = deepcopy(self.upstream.state_of_data())\n",
      "        upstream_condition.update(self.__class__.outcome)\n",
      "        return upstream_condition\n",
      "    \n",
      "    def check_preconditions(self):\n",
      "        requirements = self.__class__.__dict__.get('requirements')\n",
      "        if requirements:\n",
      "            condition = self.upstream.state_of_data()\n",
      "            for k,v in requirements.iteritems():\n",
      "                if not condition.has_key(k):\n",
      "                    raise FailedPreconditionError()\n",
      "                if condition[k] != v:\n",
      "                    raise FailedPreconditionError()                    \n",
      "\n",
      "    def run(self):\n",
      "        \"\"\"Pulls data through the pipeline.  Useful if we just want to calculate \n",
      "        some stats.\"\"\"\n",
      "        for _ in self.process():\n",
      "            pass\n",
      "\n",
      "            \n",
      "class AddOne(Node):\n",
      "    outcome = {'added_one': True}\n",
      "    requirements = {'multiplied_by_ten': True}\n",
      "    \n",
      "    def process(self):\n",
      "        self.check_preconditions()\n",
      "        self.results = 0\n",
      "        for i in self.upstream.process():\n",
      "            output = i+1\n",
      "            self.results += output\n",
      "            yield output\n",
      "            \n",
      "\n",
      "class MultiplyByTen(Node):\n",
      "    outcome = {'multiplied_by_ten': True}\n",
      "\n",
      "    def process(self):\n",
      "        self.check_preconditions()\n",
      "        for i in self.upstream.process():\n",
      "            yield i*10\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 18
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "ds = DataStore([3,5,7])\n",
      "mult = MultiplyByTen(ds)\n",
      "addone = AddOne(mult)\n",
      "addone.run()\n",
      "addone.results"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 19,
       "text": [
        "307"
       ]
      }
     ],
     "prompt_number": 19
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Train disaggregator\n",
      "\n",
      "for meter in metergroup.meters:\n",
      "    preprocessing = [Clip(), Resample(5)]\n",
      "    for chunk in meter.power_series(preprocessing=preprocessing):\n",
      "        disaggregator.train(meter, chunk)\n",
      "    \n",
      "        # and if the disaggregator needs aggregate power as well:\n",
      "        mains_chunk = mains.power_series(periods=[chunk.timeframe]).next()\n",
      "        disaggregator.train(meter, chunk, mains_chunk)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 20
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Disaggregation and metrics (via disk)\n",
      "\n",
      "for chunk in mains.power_series():\n",
      "    appliance_estimates_for_chunk = disaggregator.disaggregate(chunk) # get dataframe\n",
      "    append_to_hdf(appliance_estimates_for_chunk)\n",
      "    \n",
      "# Metrics - take two MeterGroups?  \n",
      "# One for ground truth, one for disag estimates\n",
      "\n",
      "    "
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}